package com.feri.tiantian.utils;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.List;

public class JedisUtils {
    private static JedisPool pool;
    static {
        JedisPoolConfig config=new JedisPoolConfig();
        config.setMaxIdle(30);
        pool=new JedisPool(config,"121.40.147.134",6380,10000,"Qfy_0000");
    }
    //新增内容
    public static void setStr(String key,String value,long seconds){
        Jedis jedis=pool.getResource();
        jedis.setex(key, seconds, value);
        pool.returnResource(jedis);
    }
    public static void setStr(String key,String value){
        Jedis jedis=pool.getResource();
        jedis.set(key, value);
        pool.returnResource(jedis);
    }
    public static void setSet(String key,String value){
        Jedis jedis=pool.getResource();
        jedis.set(key, value);
        pool.returnResource(jedis);
    }

    public static void setList(String key,String... vals){
        Jedis jedis=pool.getResource();
        jedis.lpush(key, vals);
        pool.returnResource(jedis);
    }
    //获取内容
    public static List<String> getList(String key){
        Jedis jedis=pool.getResource();
        List<String> list= jedis.lrange(key,0,jedis.llen(key)-1);
        pool.returnResource(jedis);
        return list;
    }
    public static String getStr(String key){
        Jedis jedis=pool.getResource();
        String r=jedis.get(key);
        pool.returnResource(jedis);
        return r;
    }
    //删除
    public static void delSet(String key,String val){
        Jedis jedis=pool.getResource();
        jedis.srem(key,val);
        pool.returnResource(jedis);
    }
    public static void del(String... keys){
        Jedis jedis=pool.getResource();
        jedis.del(keys);
        pool.returnResource(jedis);
    }
    // esists 检查给定 key 是否存在。
    public static boolean esists(String key){
        Jedis jedis=pool.getResource();
        boolean r=jedis.exists(key);
        pool.returnResource(jedis);
        return r;
    }

    public static boolean check(String key,String value){
        Jedis jedis=pool.getResource();
        boolean r=jedis.sismember(key, value);
        pool.returnResource(jedis);
        return r;
    }
}
